/*
 * AboutDialog.java
 *
 * Displays About Dialog.
 *
 * @author Guy Wittig
 * @version 20-Nov-2005
 *
 *   This program is part of MV-Plan
 *   Copywrite 2006 Guy Wittig
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   The GNU General Public License can be read at http://www.gnu.org/licenses/licenses.html
 */

package mvplan.gui.text;

import java.awt.Frame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import mvplan.main.Mvplan;
import mvplan.util.BrowserLauncher;

/**
 *
 * @author  Guy Wittig
 */
public class AboutDialog extends JDialog {
    
    Component thisObject;
    private java.awt.Cursor oldCursor;
    private String launchURL;
    
    /** Creates new form AboutDialog */
    public AboutDialog(Frame parentFrame, String info) {

        super(parentFrame,true);
        thisObject=(Component)this;
        launchURL=Mvplan.prefs.getHomeURL();
        
        initComponents();
        setResources();
        oldCursor=getCursor();
        infoTextLabel.setText(info);
        java.awt.Font infoFont = new java.awt.Font("Tahoma", 0, 11);
        infoTextLabel.setFont(infoFont);
        updateLabel1.setFont(infoFont);
        updateLabel2.setFont(infoFont);
        updateCheckBox.setFont(infoFont);
        
        // Set initial state

        updateCheckFreq=Mvplan.prefs.getUpdateCheckFrequency();
                 
        updateCheckBox.setSelected(!Mvplan.prefs.isUpdateCheckDisable());      
        
        setUpdateEnable(updateCheckBox.isSelected());
        
        updateFrequency.setText( String.valueOf(updateCheckFreq));
                
        // Set ESCAPE key to close dialog
        KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction(){
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
        };        
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
        getRootPane().getActionMap().put("ESCAPE",escapeAction);          
        pack();
        setLocationRelativeTo(parentFrame);
        getRootPane().setDefaultButton(okButton);
        setVisible(true);
    }
    
    private void setUpdateEnable(boolean b){
        updateLabel1.setEnabled(b);
        updateLabel2.setEnabled(b);
        updateFrequency.setEnabled(b);                
    }
    
    private void setResources() {
      
        okButton.setText(Mvplan.getResource("mvplan.ok.text"));
        okButton.setToolTipText(Mvplan.getResource("mvplan.ok.tip"));               
        logoLabel.setToolTipText(Mvplan.getResource("mvplan.gui.aboutDialog.logo.tip"));        
        updateCheckBox.setText(Mvplan.getResource("mvplan.gui.aboutDialog.check.text"));
        updateLabel1.setText(Mvplan.getResource("mvplan.gui.aboutDialog.every.text"));
        updateFrequency.setToolTipText(Mvplan.getResource("mvplan.gui.aboutDialog.updateFrequency.tip"));        
        updateLabel2.setText(Mvplan.getResource("mvplan.gui.aboutDialog.days.text"));        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        infoPanel = new javax.swing.JPanel();
        logoPanel = new javax.swing.JPanel();
        logoLabel = new javax.swing.JLabel();
        updatePanel = new javax.swing.JPanel();
        updateCheckBox = new javax.swing.JCheckBox();
        updateLabel1 = new javax.swing.JLabel();
        updateFrequency = new javax.swing.JTextField();
        updateLabel2 = new javax.swing.JLabel();
        infoTextLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("About");
        setModal(true);
        setName("About"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        okButton.setText("<OK>");
        okButton.setActionCommand("ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        getContentPane().add(buttonPanel, gridBagConstraints);

        infoPanel.setMinimumSize(new java.awt.Dimension(150, 150));
        infoPanel.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(infoPanel, gridBagConstraints);

        logoPanel.setLayout(new java.awt.GridBagLayout());

        logoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mvplan/resources/mvplan64.gif"))); // NOI18N
        logoLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        logoLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        logoLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                logoLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                logoLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                logoLabelMouseExited(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(20, 10, 10, 10);
        logoPanel.add(logoLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        getContentPane().add(logoPanel, gridBagConstraints);

        updatePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        updateCheckBox.setText("<Check>");
        updateCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        updateCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateCheckBoxActionPerformed(evt);
            }
        });
        updatePanel.add(updateCheckBox);

        updateLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        updateLabel1.setText("<every>");
        updatePanel.add(updateLabel1);

        updateFrequency.setColumns(3);
        updateFrequency.setText("7");
        updatePanel.add(updateFrequency);

        updateLabel2.setText("<days>");
        updatePanel.add(updateLabel2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        getContentPane().add(updatePanel, gridBagConstraints);

        infoTextLabel.setText("<INFO>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(infoTextLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void logoLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logoLabelMouseClicked
        BrowserLauncher.openURL(launchURL);
    }//GEN-LAST:event_logoLabelMouseClicked

    private void logoLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logoLabelMouseExited
        setCursor(oldCursor);
    }//GEN-LAST:event_logoLabelMouseExited

    private void logoLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logoLabelMouseEntered
        java.awt.Cursor handCursor = new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR);
        setCursor(handCursor);
    }//GEN-LAST:event_logoLabelMouseEntered

    private void updateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateCheckBoxActionPerformed
        setUpdateEnable(updateCheckBox.isSelected());

    }//GEN-LAST:event_updateCheckBoxActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // Write preferences
        int i;
        try {
            i = Integer.parseInt(updateFrequency.getText());
            if(i < 0 || i > 365)    i=7;    
        } catch (NumberFormatException ex) {
            i = 7; // Default 
        }
        Mvplan.prefs.setUpdateCheckFrequency(i);       
        Mvplan.prefs.setUpdateCheckDisable(!updateCheckBox.isSelected());                       
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JLabel infoTextLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JPanel logoPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JCheckBox updateCheckBox;
    private javax.swing.JTextField updateFrequency;
    private javax.swing.JLabel updateLabel1;
    private javax.swing.JLabel updateLabel2;
    private javax.swing.JPanel updatePanel;
    // End of variables declaration//GEN-END:variables
    private int updateCheckFreq;
    

}
